<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="../vendors/jquery/dist/jquery.min.js"></script>
  <script src="../vendors/bootstrap/extend/Message_Bootstrap.min.js"></script>
  <script src="../vendors/bootstrap/extend/Common.js"></script>
</head>
<body>
<!--<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"-->
     <!--aria-hidden="true">-->
  <!--<div class="modal-dialog">-->
    <!--<div class="modal-content">-->
      <!--<div class="modal-header">-->
        <!--<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>-->
        <!--<h4 class="modal-title">新增环境变量文件</h4>-->
      <!--</div>-->
      <!--<div class="modal-body">-->
        <!--<div class="input-group">-->
          <!--<span class="input-group-addon">名称</span>-->
          <!--<input type="text" class="form-control" id="name" placeholder="请输入环境变量名称">-->
        <!--</div>-->
      <!--</div>-->
      <!--<div class="modal-footer">-->
        <!--<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>-->
        <!--<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="checkSelectItems()">保存</button>-->
      <!--</div>-->
    <!--</div>&lt;!&ndash; /.modal-content &ndash;&gt;-->
  <!--</div>&lt;!&ndash; /.modal &ndash;&gt;-->
<!--</div>-->
<table>
  <tr>
    <td>名称：</td>
    <td><input type="text" name="name" id="name"/></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="button" value="提交" onclick="checkSelectItems()" class="btn btn-default"/></td>
  </tr>
</table>
<script>
  $(function () {
    var oldname = GetQueryString("name");
    if (oldname != null) {
      $("#name").val(oldname);
    }
  })
  function checkSelectItems() {
    var oldname = GetQueryString("name");
    if (oldname != null) {
      var storage = window.localStorage;
      var a1 = storage.kuberenvir;
      var lstobj = JSON.parse(a1);
      for (var i = 0; i < lstobj.Data.length; i++) {
        if (lstobj.Data[i].name == oldname) {
          lstobj.Data[i].name = $("#name").val()
        }
      }
      storage.kuberenvir = JSON.stringify(lstobj);
      window.parent.window.timedloadenvir();
//      $.modalzClose();
      /*  $.ajax({
       type: "put",
       url: "/api/kubernamespace/edit/"+oldname+"/"+$("#name").val(),
       dataType: "json",
       success: function (res) {
       window.parent.window.dataload();
       window.parent.window.loadmeun();
       $.modalzClose();
       },
       error: function (res) {
       $.messageBox(res.responseText, "error");
       }
       })*/
    }
    else {
      var storage = window.localStorage;
      var a1 = storage.kuberenvir;
      var lstobj = JSON.parse(a1);
      var obj = new Object();
      obj.creationTimestamp = getNowFormatDate();
      obj.name = $("#name").val();
      obj.resourceVersion = Math.round(Math.random() * 1000);
      obj.status = "Active";
      obj.uid = generateMixed(8) + "-" + generateMixed(4) + "-" + generateMixed(4) + "-" + generateMixed(8);
      lstobj.Data.push(obj);
      storage.kuberenvir = JSON.stringify(lstobj);
      window.parent.window.timedloadenvir();
//      $.modalzClose();
      /* $.ajax({
       type: "post",
       url: "/api/kubernamespace/add",
       dataType: "json",
       data: {"name": $("#name").val()},
       success: function (res) {
       window.parent.window.dataload();
       window.parent.window.loadmeun();
       $.modalzClose();
       },
       error: function (res) {
       $.messageBox(res.responseText, "error");
       }
       })*/
    }

  }

  function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return null;
  }
  function generateMixed(n) {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'c', 'd', 'e', 'b', 'f'];
    var res = "";
    for (var i = 0; i < n; i++) {
      var id = Math.ceil(Math.random() * 15);
      res += chars[id];
    }
    return res;
  }
  function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
      + " " + date.getHours() + seperator2 + date.getMinutes()
      + seperator2 + date.getSeconds();
    return currentdate;
  }
</script>
</body>
</html>